package com.hc.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hc.bean.Result;
import com.hc.bean.ResultUtil;
import com.hc.domain.User;
import com.hc.ex.GlobalException;
import com.hc.mapper.UserMapper;
import com.hc.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @GetMapping("/res")
    public Result res(){
        return ResultUtil.success(200,"ok");
    }

    @PreAuthorize("hasRole('admin')")
    @GetMapping("/fun1")
    public String fun1(){
        System.out.println("fun1");
        return "fun1";
    }

    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/fun2")
    public String fun2(){
        System.out.println("fun2");
        return "fun2";
    }

    @GetMapping("/f1")
    public void f1() { //测试
        System.out.println(userService);
        System.out.println(userMapper);
    }

    @GetMapping("/f2")
    public void f2() { //测试
        Wrapper<User> queryWrapper = new QueryWrapper<>();
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

    @GetMapping("/f3")
    public void f3() { //测试
        System.out.println(3 / 0);
    }

    @GetMapping("/f4")
    public void f4() { //测试
        throw new GlobalException(123, "这可能是一个异常");
    }

    //请求 http://localhost/blog/f5?gender=asdf
    @GetMapping("/f5")
    public void f5(User user) { //测试
        System.out.println("fffffffffff5555555555555: " + user);
    }

    //@CrossOrigin
    //@CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
    @GetMapping("/f11")
    public String f11(Integer pageNum, Integer pageSize) {
        return pageNum + " : " + pageSize;
    }

    @GetMapping("/f12")
    public String f12(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        String token = request.getHeader("token");
        return pageNum + " : " + pageSize + " : " + token;
    }

    @PostMapping("/f21")
    public String f21(@RequestBody String param) {
        return param;
    }

    @PostMapping("/f22")
    public String f22(String username, String password) {
        return username + " : " + password;
    }

    @PostMapping("/f23")
    public String f23(Integer aa, Integer bb, @RequestBody String query) {
        return aa + ": " + bb + ": " + query;
    }




}
